package com.changgou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {


    @Autowired
    private TemplateEngine templateEngine;

    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private CategoryFeign categoryFeign;


    @Value("${pagepath}")
    private String pagePath;

    /**
     * 生成静态化页面
     *
     * @param spuId
     */
    @Override
    public void generateItemPage(String spuId) {

        //1.获取context对象存放商品
        Context context = new Context();
        Map<String, Object> dataMap = this.getDataMap(spuId);
        context.setVariables(dataMap);


        //2.获取商品详情页生成位置
        File file = new File(pagePath);
        if (!file.exists()) {
            file.mkdir();
        }
        Writer out = null;
        File filePath = new File(file + File.separator + spuId + ".html");
        try {
            out = new PrintWriter(filePath);
            templateEngine.process("item", context, out);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {

            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    private Map<String, Object> getDataMap(String spuId) {

        //1.获取spu信息
        HashMap<String, Object> dataMap = new HashMap<>();
        Spu spu = spuFeign.findById(spuId).getData();
        dataMap.put("spu", spu);

        //2.获取商品图片信息
        if (spu != null) {
            if (StringUtils.isNotEmpty(spu.getImages())) {
                dataMap.put("imageList", spu.getImages().split(","));
            }
        }

        //3.获取商品分类信息
        if (spu != null) {
            Category category1 = categoryFeign.findById(spu.getCategory1Id()).getData();
            Category category2 = categoryFeign.findById(spu.getCategory2Id()).getData();
            Category category3 = categoryFeign.findById(spu.getCategory3Id()).getData();
            dataMap.put("category1", category1);
            dataMap.put("category2", category2);
            dataMap.put("category3", category3);
        }

        //4.获取sku信息
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        dataMap.put("skuList", skuList);

        //5.商品规格信息
        if (spu != null) {
            Map specMap = JSON.parseObject(spu.getSpecItems(), Map.class);
            dataMap.put("specificationList", specMap);
        }

        return dataMap;
    }
}
